package Vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

import Controller.CtrlAppli;
import Modele.Case;
import Modele.Eclaireur;
import Modele.Fourmi;
import Modele.Fourmiliere;
import Modele.Monde;
import Modele.Ouvrier;
import Modele.Position;
import Modele.Reine;

public class PanelGrille extends JPanel {
	Map<JButton,Position> mapBoutons;
	CtrlAppli ctrl_appli;
	public PanelGrille(CtrlAppli _ctrl_appli)
	{
		this(8,8,_ctrl_appli);
	}

	public PanelGrille(int i, int j,CtrlAppli _ctrl_appli) {
		ctrl_appli = _ctrl_appli;
		this.setLayout(new GridLayout(i,j));
		mapBoutons = new HashMap<JButton,Position>();
		//Border blackLine = BorderFactory.createLineBorder(Color.BLACK,1);
		for(int iGrille = 0 ; iGrille<i;iGrille++)
			for(int jGrille = 0 ; jGrille<j;jGrille++)
			{
				JButton boutonGrille = new JButton();
				mapBoutons.put(boutonGrille, new Position(iGrille, jGrille));
				if(this.ctrl_appli.getCtrl_monde().getMonde().getCaseFromPosition(new Position(iGrille, jGrille))==null)
					this.ctrl_appli.getCtrl_monde().getMonde().setCaseAtPosition(iGrille, jGrille, new Case(0,0,null,0,this.ctrl_appli.getCtrl_monde().getMonde()));
				boutonGrille.addActionListener(new ajoutListener());
				boutonGrille.setBorder(BorderFactory.createEmptyBorder());
				boutonGrille.setBackground(Color.WHITE);
				boutonGrille.setHorizontalTextPosition(JButton.CENTER);
				this.add(boutonGrille);
			}
		this.setVisible(true);
	}
	
	
	public void raffraichirInterface(Monde monde)
	{
		for(Entry<JButton,Position> entry : mapBoutons.entrySet())
		{
			paintButtonFromCase(entry.getKey(), monde.getCaseFromPosition(entry.getValue()));
		}
		invalidate();
	}
	
	public void paintButtonFromCase(JButton button,Case _case)
	{
		if(_case!=null)
		{
			if(_case.getFourmi()!=null)
			{
				if(!_case.getFourmi().isEmpty())
				{
					int nbReine =0,nbEcl = 0,nbOuv =0;
					for(int i=0;i<_case.getFourmi().size();i++)
					{
						if(_case.getFourmi().get(i) instanceof Reine)
							nbReine++;
						else if(_case.getFourmi().get(i) instanceof Ouvrier)
							nbOuv++;
						else if(_case.getFourmi().get(i) instanceof Eclaireur)
							nbEcl++;
					}
					if(nbReine>0)
					{
						ImageIcon iconR = new ImageIcon(PanelGrille.class.getResource("../img/Reine.png"));
						Image imgR = iconR.getImage().getScaledInstance(button.getHeight(), button.getWidth(), Image.SCALE_FAST);
						button.setIcon(new ImageIcon(imgR));
						button.setLayout( new BorderLayout() );
					}
					else if(nbOuv>0&&nbEcl>0)
					{
						ImageIcon iconM = new ImageIcon(PanelGrille.class.getResource("../img/Mix.png"));
						Image imgM = iconM.getImage().getScaledInstance(button.getHeight(), button.getWidth(), Image.SCALE_FAST);
						button.setIcon(new ImageIcon(imgM));
						button.setLayout( new BorderLayout() );
					}
					else if(nbOuv>0)
					{
						ImageIcon iconO = new ImageIcon(PanelGrille.class.getResource("../img/Ouvrier.png"));
						Image imgO = iconO.getImage().getScaledInstance(button.getHeight(), button.getWidth(), Image.SCALE_FAST);
						button.setIcon(new ImageIcon(imgO));
						button.setLayout( new BorderLayout() );
					}
					else if(nbEcl>0)
					{
						ImageIcon iconE = new ImageIcon(PanelGrille.class.getResource("../img/Eclaireur.png"));
						Image imgE = iconE.getImage().getScaledInstance(button.getHeight(), button.getWidth(), Image.SCALE_FAST);
						button.setIcon(new ImageIcon(imgE));
						button.setLayout( new BorderLayout() );
					}
					else 
					{
						button.setIcon(null);
						button.setBackground(Color.WHITE);
					}
				}
				else if(_case.hasNourriture())
				{
					ImageIcon iconN = new ImageIcon(PanelGrille.class.getResource("../img/nourriture.jpg"));
					Image imgN = iconN.getImage().getScaledInstance(button.getHeight(), button.getWidth(), Image.SCALE_FAST);
					button.setIcon(new ImageIcon(imgN));
					button.setLayout(new BorderLayout());
				}else if(_case.getNiveauPheromone()>0)
				{
					button.setIcon(null);
					button.setText(".");
					button.setForeground(Color.RED);
				}else if (_case.getNiveauTerrain() == 255)
				{
					ImageIcon iconT = new ImageIcon(PanelGrille.class.getResource("../img/obstacle.jpg"));
					Image imgT = iconT.getImage().getScaledInstance(button.getHeight(), button.getWidth(), Image.SCALE_FAST);
					button.setIcon(new ImageIcon(imgT));
					button.setLayout(new BorderLayout());
				}else if (_case.getNiveauTerrain()<255)
				{
					button.setText("");
					button.setIcon(null);
					button.setBackground(new Color(255-_case.getNiveauTerrain(),255-_case.getNiveauTerrain(),255-_case.getNiveauTerrain()));
				}
				else
				{
					button.setText("");
					button.setIcon(null);
					button.setBackground(Color.WHITE);
				}
			}
			else
			{
				if(_case.hasNourriture())
				{
					ImageIcon iconN = new ImageIcon(PanelGrille.class.getResource("../img/nourriture.jpg"));
					Image imgN = iconN.getImage().getScaledInstance(button.getHeight(), button.getWidth(), Image.SCALE_FAST);
					button.setIcon(new ImageIcon(imgN));
					button.setLayout(new BorderLayout());
				}else if(_case.getNiveauPheromone()>0)
				{
					button.setIcon(null);
					button.setText(".");
					button.setForeground(Color.RED);
				}else if (_case.getNiveauTerrain() == 255)
				{
					ImageIcon iconT = new ImageIcon(PanelGrille.class.getResource("../img/obstacle.jpg"));
					Image imgT = iconT.getImage().getScaledInstance(button.getHeight(), button.getWidth(), Image.SCALE_FAST);
					button.setIcon(new ImageIcon(imgT));
					button.setLayout(new BorderLayout());
				}else if (_case.getNiveauTerrain()<255)
				{
					button.setText("");
					button.setIcon(null);
					button.setBackground(new Color(255-_case.getNiveauTerrain(),255-_case.getNiveauTerrain(),255-_case.getNiveauTerrain()));
				}
				else
				{
					button.setText("");
					button.setIcon(null);
					button.setBackground(Color.WHITE);
				}
			}
			
			if(_case instanceof Fourmiliere)
			{
				button.setBackground(Color.WHITE);
				ImageIcon iconF = new ImageIcon(PanelGrille.class.getResource("../img/fourmiliere.png"));
				Image imgF = iconF.getImage().getScaledInstance(button.getHeight(), button.getWidth(), Image.SCALE_FAST);
				button.setIcon(new ImageIcon(imgF));
				button.setLayout( new BorderLayout() );
			}
		}
	}
	
	class ajoutListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) {
			FenSelectAjout fenAjout;
			Position p = mapBoutons.get(e.getSource());
			if(p!=null)
			{
				 fenAjout= new FenSelectAjout(p,PanelGrille.this.ctrl_appli);
				 PanelGrille.this.ctrl_appli.ajoutFenetre(fenAjout);
				 fenAjout.setVisible(true);
			}
				 
		}
		
	}
}
